//
//  UIViewController+NavigationBarButton.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/14.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIViewController+NavigationBarButton.h"

#define FUNCTION_IOS_VERSION_BEGIN(ver)  \
NSString *curVersion = [[UIDevice currentDevice] systemVersion]; \
if ([curVersion compare:@#ver] == NSOrderedDescending || [curVersion compare:@#ver] == NSOrderedSame) {
#define FUNCTION_IOS_VERSION_ELSE } else {
#define FUNCTION_IOS_VERSION_END }

#if ! __has_feature(objc_arc)

#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }

#else

#define RELEASE_SAFELY(__POINTER) { __POINTER = nil; }

#endif

#define _IsNil(id)            {if (id == nil) {return NO;}  return YES}


#define Width_Max           65.0f
#define Button_Size         44.0f

#define EdgeInsets_IMAGE    -10.f
#define EdgeInsets_TEXT     -15.0f

#define Font_Title          [UIFont systemFontOfSize:16.0f]

static NSString *kClickLeftBlockKey = @"ClickLeftBlockKey";
static NSString *kClickRightBlockKey = @"ClickRightBlockKey";

@implementation UIViewController (NavigationBarButton)

#pragma mark - Setting && Getting Click Block

- (void)setClickLeftButtonBlock:(void (^)(UIButton *))clickLeftButtonBlock {
    [self willChangeValueForKey:kClickLeftBlockKey];
    objc_setAssociatedObject(self, &kClickLeftBlockKey, clickLeftButtonBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:kClickLeftBlockKey];
}

- (void (^)(UIButton *))clickLeftButtonBlock {
    return objc_getAssociatedObject(self, &kClickLeftBlockKey);
}


- (void)setClickRightButtonBlock:(void (^)(UIButton *))clickRightButtonBlock {
    [self willChangeValueForKey:kClickRightBlockKey];
    objc_setAssociatedObject(self, &kClickRightBlockKey, clickRightButtonBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:kClickRightBlockKey];
}

- (void (^)(UIButton *))clickRightButtonBlock {
    return objc_getAssociatedObject(self, &kClickRightBlockKey);
}


#pragma mark - Block Target

- (void)blockClickLeftButton:(UIButton *)button {
    if (self.clickLeftButtonBlock) {
        self.clickLeftButtonBlock(button);
    }
}

- (void)blockClickRightButton:(UIButton *)button {
    if (self.clickRightButtonBlock) {
        self.clickRightButtonBlock(button);
    }
}


#pragma mark - Setting NavigationBar LeftButton

//设置左导航按钮---有图无文字类型
- (void)setNavigationBarLeftButtonWithImage:(NSString *)imageName
                                 hightImage:(NSString *)hImageName
                                     action:(SEL)action
                                    command:(RACCommand *)command {
    [self setupNavLeftButtonWithImage:imageName hightImage:hImageName action:action open:YES command:command];
}

- (void)setNavigationBarLeftButtonWithImage:(NSString *)imageName
                                 hightImage:(NSString *)hImageName
                                     action:(SEL)action
                                       open:(BOOL)open {
    [self setupNavLeftButtonWithImage:imageName hightImage:hImageName action:action open:open command:nil];
}

- (void)setNavigationBarLeftButtonWithImage:(NSString *)imageName
                                 hightImage:(NSString *)hImageName {
    [self setupNavLeftButtonWithImage:imageName hightImage:hImageName action:nil open:YES command:nil];
}

- (void)setupNavLeftButtonWithImage:(NSString *)imageName
                         hightImage:(NSString *)hImageName
                             action:(SEL)action
                               open:(BOOL)open
                            command:(RACCommand *)command {
    UIButton *btn   = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame       = CGRectMake(0, 0, Button_Size, Button_Size);
    
    if (imageName && hImageName) {
        [btn setImage:CREATE_IMG(imageName) forState:UIControlStateNormal];
        [btn setImage:CREATE_IMG(hImageName) forState:UIControlStateHighlighted];
    }
    
    if (!action) {
        [btn addTarget:self action:@selector(blockClickLeftButton:) forControlEvents: UIControlEventTouchUpInside];
    } else {
        [btn addTarget:self action:action forControlEvents: UIControlEventTouchUpInside];
    }
    
    FUNCTION_IOS_VERSION_BEGIN(7.0)
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, EdgeInsets_IMAGE, 0, 0);
    
    FUNCTION_IOS_VERSION_END
    
    
    UIBarButtonItem *item                   = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem   = item;
    item.rac_command = command;
    
    RELEASE_SAFELY(item);
    
    if (open) {
        btn.userInteractionEnabled = YES;
    }else{
        btn.userInteractionEnabled = NO;
    }
    
}




//设置左导航按钮---有title无图类型
- (void)setNavigationBarLeftButtonWithTitle:(NSString *)title
                                     action:(SEL)action
                                       open:(BOOL)open {
    [self setupNavLeftButtonWithTitle:title action:action open:open command:nil];
}


- (void)setNavigationBarLeftButtonWithTitle:(NSString *)title
                                     action:(SEL)action
                                    command:(RACCommand *)command {
    [self setupNavLeftButtonWithTitle:title action:action open:YES command:command];
}

- (void)setNavigationBarLeftButtonWithTitle:(NSString *)title {
    [self setupNavLeftButtonWithTitle:title action:nil open:YES command:nil];
}

- (void)setupNavLeftButtonWithTitle:(NSString *)title
                             action:(SEL)action
                               open:(BOOL)open
                            command:(RACCommand *)command {
    CGSize size   = [title sizeWithAttributes:@{ NSFontAttributeName : Font_Title }];
    int width     = size.width > Width_Max ? size.width : Width_Max;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame     = CGRectMake(0, 0, width, Button_Size);
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateHighlighted];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [btn.titleLabel setFont:Font_Title];
    if (!action) {
        [btn addTarget:self action:@selector(blockClickLeftButton:) forControlEvents: UIControlEventTouchUpInside];
    } else {
        [btn addTarget:self action:action forControlEvents: UIControlEventTouchUpInside];
    }
    
    FUNCTION_IOS_VERSION_BEGIN(7.0)
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, EdgeInsets_TEXT, 0, 0);
    
    FUNCTION_IOS_VERSION_END
    
    UIBarButtonItem *item                   = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem   = item;
    item.rac_command = command;
    
    RELEASE_SAFELY(item);
    
    if (open) {
        btn.userInteractionEnabled = YES;
    } else {
        btn.userInteractionEnabled = NO;
    }
    
}
/**
 *  删除navigationBar的左按钮
 */
- (void)removeNavigationBarLeftButton {
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setHidesBackButton:YES];
}


#pragma mark - Setting NavigationBar RightButton

//设置右导航按钮---有图片无文字类型
- (void)setNavigationBarRightButtonWithImage:(NSString *)imageName
                                  hightImage:(NSString *)hImageName
                                      action:(SEL)action
                                        open:(BOOL)open {
    [self setupNavRightButtonWithImage:imageName hightImage:hImageName action:action open:open command:nil];
}


- (void)setNavigationBarRightButtonWithImage:(NSString *)imageName
                                  hightImage:(NSString *)hImageName
                                      action:(SEL)action
                                     command:(RACCommand *)command {
    [self setupNavRightButtonWithImage:imageName hightImage:hImageName action:action open:YES command:command];
}


- (void)setNavigationBarRightButtonWithImage:(NSString *)imageName
                                  hightImage:(NSString *)hImageName {
    [self setupNavRightButtonWithImage:imageName hightImage:hImageName action:nil open:YES command:nil];
}


- (void)setupNavRightButtonWithImage:(NSString *)imageName
                          hightImage:(NSString *)hImageName
                              action:(SEL)action
                                open:(BOOL)open
                             command:(RACCommand *)command {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =CGRectMake(0, 0, Button_Size, Button_Size);
    if (imageName && hImageName) {
        [btn setImage:CREATE_IMG(imageName) forState:UIControlStateNormal];
        [btn setImage:CREATE_IMG(hImageName) forState:UIControlStateHighlighted];
    }
    
    if (!action) {
        [btn addTarget:self action:@selector(blockClickRightButton:) forControlEvents: UIControlEventTouchUpInside];
    } else {
        [btn addTarget:self action:action forControlEvents: UIControlEventTouchUpInside];
    }
    
    FUNCTION_IOS_VERSION_BEGIN(7.0)
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, EdgeInsets_IMAGE);
    
    FUNCTION_IOS_VERSION_END
    
    UIBarButtonItem *item                   = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem  = item;
    item.rac_command = command;
    
    RELEASE_SAFELY(item);
    
    if (open) {
        btn.userInteractionEnabled = YES;
    } else {
        btn.userInteractionEnabled = NO;
    }
    
}

//设置右导航按钮---有title没有图片
- (void)setNavigationBarRightButtonWithTitle:(NSString *)title
                                      action:(SEL)action
                                     command:(RACCommand *)command {
    [self setupNavigationBarRightButtonWithTitle:title action:action open:YES command:command];
}

- (void)setNavigationBarRightButtonWithTitle:(NSString *)title
                                      action:(SEL)action
                                        open:(BOOL)open {
    [self setupNavigationBarRightButtonWithTitle:title action:action open:open command:nil];
}


- (void)setNavigationBarRightButtonWithTitle:(NSString *)title {
    [self setupNavigationBarRightButtonWithTitle:title action:nil open:YES command:nil];
}

- (void)setupNavigationBarRightButtonWithTitle:(NSString *)title
                                        action:(SEL)action
                                          open:(BOOL)open
                                       command:(RACCommand *)command {
    CGSize size     = [title sizeWithAttributes:@{ NSFontAttributeName : Font_Title }];
    int width       = size.width > Width_Max ? size.width : Width_Max;
    UIButton *btn   = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame       = CGRectMake(0, 0, width, Button_Size);
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateHighlighted];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    [btn.titleLabel setFont:Font_Title];
    if (!action) {
        [btn addTarget:self action:@selector(blockClickRightButton:) forControlEvents: UIControlEventTouchUpInside];
    } else {
        [btn addTarget:self action:action forControlEvents: UIControlEventTouchUpInside];
    }
    
    FUNCTION_IOS_VERSION_BEGIN(7.0)
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, EdgeInsets_TEXT);
    
    FUNCTION_IOS_VERSION_END
    
    UIBarButtonItem *item                   = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem  = item;
    item.rac_command = command;
    
    RELEASE_SAFELY(item);
    
    if (open) {
        btn.userInteractionEnabled = YES;
    } else {
        btn.userInteractionEnabled = NO;
    }
}
/**
 *  删除navigationBar的右按钮
 */
- (void)removeNavigationBarRightButton {
    self.navigationItem.rightBarButtonItem = nil;
    [self.navigationItem setHidesBackButton:YES];
}


@end

